﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace com.guysherman.simulclass
{
    class Program
    {
        private static string binaryPath;
        private static string outputDirectory;
        private static string headerPreamble;
        private static string includeFile;
        private static string bodyIncludeFile;

        static void Main(string[] args)
        {
            Console.WriteLine("SuperNova: WrapperGenerator v0.1");
            if (!validateArgs(args))
            {
                printUsage();
                return;
            }

            AssemblyScanner a = new AssemblyScanner
            {
                AssemblyPath = binaryPath,
                OutputDirectory = outputDirectory,
                HeaderPreamble = headerPreamble,
                IncludeFile = includeFile,
                BodyIncludeFile = bodyIncludeFile
                
            };
            a.GenerateClassesInAssembly();


            done();
        }

        private static bool validateArgs(string[] args)
        {
            if (args.Length < 5)
            {
                return false;
            }

            string arg = string.Empty;
            string workingDirectory = Directory.GetCurrentDirectory();
            
            // Get the absolute path to the output directory
            // and create the directory if it doesn't exist
            arg = Path.GetFullPath(args[1]);

            if (!Directory.Exists(arg))
            {
                Directory.CreateDirectory(arg);
                
            }
            outputDirectory = arg;

            // Get the absolute path to the subject assembly and 
            // check if it exists.
            arg = Path.GetFullPath(args[0]);
            if (!File.Exists(arg))
            {
                Console.WriteLine(string.Format("Error: file not found: {0}", arg));
                return false;
            }
            else
            {
                Console.WriteLine(string.Format("Now scanning: {0}", arg));
                binaryPath = arg;
            }

            // Get the absolute path to the preamble file and check if it exists
            arg = Path.GetFullPath(args[2]);
            if (!File.Exists(arg))
            {
                Console.WriteLine(string.Format("Error: preamble file not found: {0}", arg));
                return false;
            }
            else
            {
                headerPreamble = File.ReadAllText(arg);
            }

            // Get the absolute path to the header includes file and check if it exists
            arg = Path.GetFullPath(args[3]);
            if (!File.Exists(arg))
            {
                Console.WriteLine(string.Format("Error: header includes file not found: {0}", arg));
                return false;
            }
            else
            {
                includeFile = File.ReadAllText(arg);
            }

            // Get the absolute path to the cpp includes file and check if it exists
            arg = Path.GetFullPath(args[4]);
            if (!File.Exists(arg))
            {
                Console.WriteLine(string.Format("Error: body includes file not found: {0}", arg));
                return false;
            }
            else
            {
                bodyIncludeFile = File.ReadAllText(arg);
            }

            return true;
        }

        private static string GetAbsolutePath(string arg, string workingDirectory)
        {
            if (!Path.IsPathRooted(arg))
            {
                arg = Path.Combine(workingDirectory, arg);
            }
            return arg;
        }

        private static void printUsage()
        {
            Console.WriteLine("Usage: wrappergen <binary> <output-dir> <preamble-file> <header-include-file> <body-include-file>");
        }

        private static void done()
        {
            Console.WriteLine("Press any key to continue...");
            Console.Read();
        }
    }
}
